In this exercise, we will familiarise ourselves with the Julia REPL and print a few things.
The great thing about REPLs is that they make learning a language a lot easier - especially a language that is at least partly intended to allow you to quickly prototype complex ideas in a few lines of code, manipulate your code, iterate until you get the desired results, then flesh it out or tidy it up.
Using the REPL isn't complicated, but it might be unusual at first if you have not used much of a similarly constructed REPL (such as Prelude, the Haskell REPL). It helps to remember a few commands and tricks for the future.
Prompt: julia>
In this mode, you interact with the Julia engine directly. Pressing Return/Enter executes the command in the current line and prints the result. You can also access the result of the last operation in the ans variable. If you don't wish for Julia to print the result, conclude your line with a ; (semicolon).
In [27]:
println("Hello World!")
In [28]:
?besselj
Out[28]:
In [29]:
; python -V
The Julia REPL uses a few key bindings that might be very familiar to those who have used *nix based systems frequently in the past. Most importantly, to exit the REPL, you can use ^D (Ctrl+D), which will also close your shell, and you can abort a current operation using ^C (Ctrl+C).
There are many more key bindings that the Julia REPL recognises, but these should be enough to get you off the ground.
In [31]:
2*2.5*π
Out[31]:
Pressing the Tab key on a LaTeX symbol name autocompletes to Unicode symbols (again, right in Jupyter):
In [33]:
√2 # \sqrt[Tab]2
Out[33]:
For all Unicode completions, check out the Unicode conversion table in the Julia documentation. Remember, you can also use Unicode symbols in saved code.
In the following, we'll be exploring a few ways to say hello to Julia. Each of these has their place in the coder's arsenal, and while you will eventually use the REPL a little less and your text editor a little more, you will probably use the REPL quite a bit to test out new ideas. Think of the REPL as your lab and the text editor as your drawing board – scientists who spend all their time in the lab eventually go mad, while those who are always at the drawing board rarely discover much!
In [35]:
"Hello, Julia!"
Out[35]:
In [34]:
v = "Hello, Julia!"
Out[34]:
In [36]:
v
Out[36]:
In [37]:
println("Hello, Julia!")
You might notice that the "Hello, Julia!" string is not printed in quotes. This is to indicate that this a system output, rather than part of the REPL's print cycle (it's an output, not an internal variable)
In [38]:
what = "Hello"
Out[38]:
In [39]:
whom = "Julia"
Out[39]:
In [40]:
string(what, ", ", whom, "!")
Out[40]:
Julia also allows for variables to be called directly within string literals via the $ prefix.
So the above is equivalent to:
In [41]:
"$what, $whom\!"
Out[41]:
And this is true even for maths (or any function!)
In [42]:
"2 plus 2 is $(2+2)."
Out[42]:
To run a Julia program, you have two options – either include it in another program or the REPL, or specify it as a positional argument in the command line.
Let's open a file in our favourite text editor, call it hello.jl (the commonly accepted file name for a Julia program), and enter:
In [44]:
println("hello world")
We now have two ways to launch it.
By opening the REPL, we can include the file. This will evaluate everything in the file, then return the result of the last valid expression.
In [48]:
include("resources/hello.jl")
Alternatively, you can merely grace your command line with your greeting to Julia by launching Julia with the appropriate argument:
In [49]:
;julia resources/hello.jl
The journey of a thousand miles begins with a single step. If REPLs are something new to you (they are rarely used with a number of OOP languages, and most people using it in Python are using functional(ish) paradigms), then this chapter was two steps at the very least. Give yourself a pat on the back and a cookie, and play around with Julia.
See you soon with the next chapter!